<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="search" />
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="createAdd">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ scope.row.status }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="{row}">
            <el-button size="mini" type="text" @click="edit(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleteUser(row.id)">删除</el-button>
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="SysUserChange"
      />
    </div>
    <!-- 添加员工 -->

    <el-dialog :title="editId?'编辑员工':'添加员工'" :visible="dialogTableVisible" class="from" width="30%" @close="cancel">
      <el-form ref="formData" :model="formData" :rules="rules">
        <el-form-item label="员工姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="formData.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="登录账号" :label-width="formLabelWidth" prop="userName">
          <el-input v-model="formData.userName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth" prop="phonenumber">
          <el-input v-model="formData.phonenumber" autocomplete="off" />
        </el-form-item>
        <span>默认密码：123456</span>
        <el-form-item label="分配角色" :label-width="formLabelWidth" prop="roleId">
          <el-select v-model="formData.roleId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="员工状态" :label-width="formLabelWidth" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="0">未启用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitAdd">{{ editId?"编辑":"添加" }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSysUserListAPI, getSysUserPermissionAPI, addSysUserAPI, getSysUserDetailAPI, editSysUserAPI, deleteSysUserAPI } from '@/api/sysUser'
export default {
  name: 'Employee',
  data() {
    return {
      editId: null,
      value: '',
      employeeList: [],
      params: {
        name: '',
        page: 1,
        pageSize: 2
      },
      total: 0,
      // 添加员工弹窗
      dialogTableVisible: false,
      formData: {
        name: '',
        userName: '',
        roleId: null,
        roleName: '',
        status: null,
        phonenumber: ''
      },
      formLabelWidth: '120px',
      // 表单检验
      rules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        phonenumber: [{
          required: true, message: '请输入联系方式', trigger: 'blur'
        },
        { validator: (rule, value, callback) => {
          const reg = /^1[3456789]\d{9}$/
          if (reg.test(value)) {
            callback()
          } else {
            callback(new Error('请输入正确的手机号'))
          }
        } }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'change' }
        ],
        roleId: [{
          required: true, message: '请选择分配角色', trigger: 'change'
        }]
      },
      // 下拉框
      options: []
    }
  },
  async mounted() {
    // 调用接口获取员工列表
    await this.getSysUserList()
    // // 获取下拉框查询权限信息
    await this.getSysUserPermission()
    // 有id时，获取详情
    if (this.id) {
      await this.getSysUserDetail()
    }
  },
  methods: {
    // 封装接口调用
    async getSysUserList() {
      const res = await getSysUserListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    // 查询员工
    search() {
      this.params.page = 1
      this.getSysUserList()
    },
    // 分页,切换页码
    SysUserChange(page) {
      // 把当前页码赋值给params中的page
      this.params.page = page
      // 调用接口获取数据，重新渲染页面
      this.getSysUserList()
    },
    // 添加员工
    createAdd() {
      this.dialogTableVisible = true
    },
    // 获取下拉框查询权限信息
    async getSysUserPermission() {
      const res = await getSysUserPermissionAPI()
      this.options = res.data
      // console.log(res)
    },
    // 提交添加员工
    submitAdd() {
      this.$refs.formData.validate(async valid => {
        if (valid) {
          if (this.editId) {
            const { name, userName, roleId, roleName, status, phonenumber } = this.formData
            await editSysUserAPI({ name, userName, roleId, roleName, status, phonenumber, id: this.editId })
            this.$message.success('编辑成功')
          } else {
            const { name, userName, roleId, roleName, status, phonenumber } = this.formData
            await addSysUserAPI({ name, userName, roleId, roleName, status, phonenumber })
            this.$message.success('添加成功')
          }
          // 关闭弹窗
          this.dialogTableVisible = false
          // 重置表单
          this.$refs.formData.resetFields()
          // 重新获取数据
          this.getSysUserList()
        }
      })
    },
    // 取消添加员工，并重置表单
    cancel() {
      this.editId = null
      this.dialogTableVisible = false
      this.$refs.formData.resetFields()
    },
    // 获取详情
    async  getSysUserDetail(id) {
      const res = await getSysUserDetailAPI(id)
      this.formData = res.data
    },
    // 编辑员工
    edit(id) {
      // 保存id
      this.editId = id
      // 关闭弹窗
      this.dialogTableVisible = true
      // 把id传给接口，获取详情
      this.getSysUserDetail(id)
    },
    // 删除员工
    async deleteUser(id) {
      await deleteSysUserAPI(id)
      this.$message.success('删除成功')
      // 重新获取数据
      this.getSysUserList()
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
.from{
  margin:  0 auto;
  .el-form-item{
    max-width: 350px;

  }
  span{
    display:flex;
    width: 200px;
    justify-content: center;
    margin-bottom: 10px;
    align-content: center;
  }
}
</style>
